package com.fortune.service.impl;

import com.fortune.dao.DdataAnalysisDao;
import com.fortune.dto.in.ddataAnalysisDto.CommodityInventoryLedgerDto;
import com.fortune.dto.in.ddataAnalysisDto.DorderSummaryDto;
import com.fortune.service.DdataAnalysisService;
import com.fortune.vo.ddateAnalysis.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/9 0009.
 */
@Service
public class DdataAnalysisServiceImpl implements DdataAnalysisService{
    @Autowired
    private DdataAnalysisDao ddataAnalysisDao;
    @Override
    public List<ClientPro> findOrderSummaryList(DorderSummaryDto dorderSummaryDto, String did) {
        List<ClientPro> list = ddataAnalysisDao.findOrderSummaryList(dorderSummaryDto,did); //查客户
        if(list.size()>0){
            for(ClientPro clientPro : list){
                float a = new Float(0);
                List<DdataProduct> list1 = ddataAnalysisDao.findMid(did);  //查MID
                List<DdataProduct> newList = new ArrayList<>();
                List<DdataProduct> list2 = ddataAnalysisDao.findPro2(did);
                for(DdataProduct ddataProduct2:list2){
                    newList.add(ddataProduct2);
                }
                for(DdataProduct ddataProduct:list1) {
                    List<DdataProduct> list3 = ddataAnalysisDao.findPro(ddataProduct.getMid());  //查mid下的商品
                    for(DdataProduct ddataProduct1:list3){
                        newList.add(ddataProduct1);
                    }
                }
                for(DdataProduct ddataProduct1 : newList){
                        Integer ddataProduct2 = ddataAnalysisDao.findNum(dorderSummaryDto,clientPro.getClientId(),ddataProduct1.getProId(),did);
                        if(ddataProduct2 == null){
                            ddataProduct1.setOrderNum(0);
                        }else{
                            ddataProduct1.setOrderNum(ddataProduct2);
                        }
                        a = a+ddataProduct1.getOrderNum();
                    }
                clientPro.setProName(newList);
                clientPro.setCount(a);
                }
            }
        return list;
    }

    @Override
    public int findOrderSummaryListCount(String did) {
        return ddataAnalysisDao.findOrderSummaryListCount(did);
    }

    @Override
    public List<ProInStock> findCommodityInventorySummaryList(DorderSummaryDto dorderSummaryDto, String did) {
        List<DdataProduct> list1 = ddataAnalysisDao.findMid(did);  //查MID
        List<DdataProduct> newList = new ArrayList<>();
        List<DdataProduct> list2 = ddataAnalysisDao.findPro2(did);
        for(DdataProduct ddataProduct2:list2){
            ddataProduct2.setMid("");
            newList.add(ddataProduct2);
        }
        for(DdataProduct ddataProduct:list1) {
            List<DdataProduct> list3 = ddataAnalysisDao.findPro(ddataProduct.getMid());  //查mid下的商品
            for(DdataProduct ddataProduct1:list3){
                newList.add(ddataProduct1);
            }
        }
        //////////////////////////////////////////////// 上面查所有符合要求的商品
        List<ProInStock> newlist = new ArrayList<>();
        for(DdataProduct ddataProduct : newList){
            ProInStock proInStock = new ProInStock();//查条形码  赋值给新LIST
            String barcode = ddataAnalysisDao.findBarcode(ddataProduct.getProId());
            proInStock.setBarcode(barcode);
            proInStock.setProId(ddataProduct.getProId());
            proInStock.setProName(ddataProduct.getProName());
            proInStock.setMid(ddataProduct.getMid());
            newlist.add(proInStock);
        }

        List<proStore> list = ddataAnalysisDao.findStore(did);//查经销商下的所有经销商仓库
        int i = 0;
        for(ProInStock proInStock:newlist){  //遍历最新的LIST
            i=i++;
            float a = 0;
            List<proStore> list3 = new ArrayList<>();
            for(proStore proStore:list){
                proStore proStore1 = new proStore();
                Float num = ddataAnalysisDao.findInStock(proStore.getStoreId(),proInStock.getProId(),did,proInStock.getMid());
                if(num == null){
                    proStore1.setNum(0);/*proStore.setNum(0);*/
                    proStore1.setStoreId(proStore.getStoreId());
                    proStore1.setStoreName(proStore.getStoreName());
                }else{
                    proStore1.setNum(num);/*proStore.setNum(num);*/
                    proStore1.setStoreId(proStore.getStoreId());
                    proStore1.setStoreName(proStore.getStoreName());
                }
                a = a + proStore1.getNum();
                list3.add(proStore1);
            }
            proInStock.setStore(list3);
            proInStock.setCount(a);
        }

        return newlist;
    }

    @Override
    public List<ExpenseAccount> findCommodityInventoryLedgerList(CommodityInventoryLedgerDto commodityInventoryLedgerDto, String did) {
        List<ExpenseAccount> list = ddataAnalysisDao.findCommodityInventoryLedgerList(commodityInventoryLedgerDto,did);
        if(list.size() > 0){
            for(ExpenseAccount expenseAccount:list){
                if(expenseAccount.getType().equals("CSHRK")){
                    expenseAccount.setType("初始化入库");
                }else if(expenseAccount.getType().equals("JHRK")){
                    expenseAccount.setType("进货入库");
                }else if(expenseAccount.getType().equals("XSCK")){
                    expenseAccount.setType("销售出库");
                }else if(expenseAccount.getType().equals("DBRK")){
                    expenseAccount.setType("调拨入库");
                }else if(expenseAccount.getType().equals("DBCK")){
                    expenseAccount.setType("调拨出库");
                }else if(expenseAccount.getType().equals("BYRK")){
                    expenseAccount.setType("报盈入库");
                }else if(expenseAccount.getType().equals("BKCK")){
                    expenseAccount.setType("报亏出库");
                }
            }
        }

        return list;
    }

    @Override
    public int findCommodityInventoryLedgerListCount(CommodityInventoryLedgerDto commodityInventoryLedgerDto, String did) {
        return ddataAnalysisDao.findCommodityInventoryLedgerListCount(commodityInventoryLedgerDto,did);
    }

}
